﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using WFA_BANHANG.ThuVien;

namespace WFA_BANHANG.HeThong.DynamicMenu
{
    public partial class frmCreateDynamicMenu : DevExpress.XtraEditors.XtraForm
    {
        private CreateDynamicMenuBLO _cdmblo;
        private byte[] image = null;
        public DataSet _dsFrmCreate;
        public int _status = 0;//insert
        public string _menuID = null;

        public frmCreateDynamicMenu()
        {
            InitializeComponent();
        }
        
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckValidate(new TextEdit[] { txtMenuID, txtMenuName, txtMenuParent, txtProjectName, txtFileName }))
                {
                    if (_status == 0)
                    {
                        if (!this.CheckExisted())
                        {
                            //assign value to object of CreateDynamicMenuBLO class
                            _cdmblo = new CreateDynamicMenuBLO(1); //process insert
                            _cdmblo.Cdmdto.MenuID = txtMenuID.Text.Trim();
                            _cdmblo.Cdmdto.MenuName = txtMenuName.Text.Trim();
                            _cdmblo.Cdmdto.MenuParent = txtMenuName.Text.Trim();
                            _cdmblo.Cdmdto.ProjectName = txtProjectName.Text.Trim();
                            _cdmblo.Cdmdto.FileName = txtFileName.Text.Trim();
                            _cdmblo.Cdmdto.ParameterName = txtParameterName.Text.Trim();
                            _cdmblo.Cdmdto.ParameterValue = txtParameterValue.Text.Trim();

                            if (picIcon.Image != null)
                            {
                                MemoryStream ms = new MemoryStream();
                                picIcon.Image.Save(ms, picIcon.Image.RawFormat);
                                image = ms.GetBuffer();
                                ms.Close();
                            }
                            else image = System.Text.Encoding.UTF8.GetBytes(String.Empty);

                            _cdmblo.Cdmdto.IconMenu = image;

                            //call method Insert
                            _cdmblo.Call();

                            ClearDataInsideControls();

                            this.Hide();
                            frmViewDynamicMenu fvdm = new frmViewDynamicMenu();
                            fvdm.LoadaDataToGrid();
                            fvdm.Show();

                        }//the end if existed
                    }//the end if status
                    else if (_status == 1) {

                        //assign value to object of CreateDynamicMenuBLO class
                        _cdmblo = new CreateDynamicMenuBLO(2); //process update
                        _cdmblo.Cdmdto.MenuID = txtMenuID.Text.Trim();
                        _cdmblo.Cdmdto.MenuName = txtMenuName.Text.Trim();
                        _cdmblo.Cdmdto.MenuParent = txtMenuName.Text.Trim();
                        _cdmblo.Cdmdto.ProjectName = txtProjectName.Text.Trim();
                        _cdmblo.Cdmdto.FileName = txtFileName.Text.Trim();
                        _cdmblo.Cdmdto.ParameterName = txtParameterName.Text.Trim();
                        _cdmblo.Cdmdto.ParameterValue = txtParameterValue.Text.Trim();

                        if (picIcon.Image != null)
                        {
                            MemoryStream ms = new MemoryStream();
                            picIcon.Image.Save(ms, picIcon.Image.RawFormat);
                            image = ms.GetBuffer();
                            ms.Close();
                        }
                        else image = System.Text.Encoding.UTF8.GetBytes(String.Empty);

                        _cdmblo.Cdmdto.IconMenu = image;

                        //call method Insert
                        _cdmblo.Call();

                        ClearDataInsideControls();

                        this.Hide();
                        frmViewDynamicMenu fvdm = new frmViewDynamicMenu();
                        fvdm.LoadaDataToGrid();
                        fvdm.Show();

                    }
                }//the end if validate
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnThietLapLai_Click(object sender, EventArgs e)
        {           
            ClearDataInsideControls();
        }
        
        //method reset all controls
        public void ClearDataInsideControls() {

            txtMenuID.Text = "";
            txtMenuName.Text = "";
            txtMenuParent.Text = "";
            txtProjectName.Text = "";
            txtFileName.Text = "";
            txtParameterName.Text = "";
            txtParameterValue.Text = "";
            picIcon.Text = "";
            txtMenuID.Focus();
        }

        //method check validate
        public bool CheckValidate(TextEdit[] te) {

            for (int i = 0; i < te.Length;i++ )
            {
                if (string.IsNullOrEmpty(te[i].Text.Trim()))
                {
                    MessageBox.Show("Có ít nhất 1 trường dữ liệu còn trống. Mời nhập thêm!!!", "Thông báo");
                    te[i].Focus();
                    return false;
                }

                if (te[i].Text.Trim().Length > 50)
                {
                    MessageBox.Show("Có ít nhất 1 trường dữ liệu có nhiều hơn 50 ký tự. Mời nhập thêm!!!", "Thông báo");
                    te[i].Focus();
                    return false;
                }
            }

            return true;
        }

        //method check data Existed
        public bool CheckExisted() {

            ADONET adonet = new ADONET();
            DataSet ds = adonet.BindToDataSet("SELECT MenuID, MenuName, MenuParent, FileName FROM ListMenu");

            foreach (DataRow dr in ds.Tables["Table"].Rows) {

                if (dr["MenuID"].ToString().Equals(txtMenuID.Text.Trim())) {
                    
                    MessageBox.Show("MenuID đã tồn tại. Mời nhập lại!","Thông báo");
                    txtMenuID.Focus();
                    return true;
                }

                if (dr["MenuName"].ToString().Equals(txtMenuName.Text.Trim())) {
                    
                    MessageBox.Show("MenuName đã tồn tại. Mời nhập lại","Thông báo");
                    txtMenuName.Focus();
                    return true;
                }

                if (dr["MenuParent"].ToString().Equals(txtMenuParent.Text.Trim())) {
                    
                    MessageBox.Show("MenuParent đã tồn tại. Mời nhập lại", "Thông báo");
                    txtMenuParent.Focus();
                    return true;
                }

                if (dr["FileName"].ToString().Equals(txtFileName.Text.Trim())) {
                    
                    MessageBox.Show("FileName đã tồn tại. Mời nhập lại", "Thông báo");
                    txtFileName.Focus();
                    return true;
                }
            }
            
            return false;
        }

        //method load data again to controls
        public void LoadDataAgain(TextEdit[] te, PictureEdit pc)
        {
            try
            {
                DataRow[] dr = _dsFrmCreate.Tables["Table"].Select("MenuID='" + _menuID + "'");
                foreach (var v in dr)
                {
                    te[0].Text = v["MenuID"].ToString();
                    te[1].Text = v["MenuName"].ToString();
                    te[2].Text = v["MenuParent"].ToString();
                    te[3].Text = v["ProjectName"].ToString();
                    te[4].Text = v["FileName"].ToString();
                    te[5].Text = v["ParameterName"].ToString();
                    te[6].Text = v["ParameterValue"].ToString();
                    pc.EditValue = v["IconMenu"];
                    
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Thông báo"); }
        }

        private void frmCreateDynamicMenu_Load(object sender, EventArgs e)
        {
            try
            {
                if (_status == 1)
                {
                    txtMenuID.Properties.ReadOnly = true;
                    txtMenuName.Focus();
                    LoadDataAgain(new TextEdit[] { txtMenuID, txtMenuName, txtMenuParent, txtProjectName, txtFileName, txtParameterName, txtParameterValue }, picIcon);
                }
                else txtMenuID.Focus();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Thông báo"); }
        }

    }//the end class
}//the end namespace